package DBLayer;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import ModelLayer.PartOrder;
import ModelLayer.Product;
import ModelLayer.SalesOrder;

public class DBPartOrder implements IFDBPartOrder {
	private Connection con;

	@Override
	public ArrayList<PartOrder> getAll(boolean retriveAssociation) {
		 return miscWhere("", retriveAssociation);
	}

	@Override
	public PartOrder findProduct(int id, boolean retriveAssociation) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int insert(PartOrder order) throws Exception {
		 //call to get the next ssn number
       
 
      int rc = -1;
	   String query="INSERT INTO SALESORDER(quantity, price, productid, orderid)  VALUES('"+
	  		order.getQuantity()  + "','"  +
		    order.getPrice()  + "','"  +
	  		order.getProduct().getId() + "','" +
	  		order.getOrder().getOrderid() + "')";
           
           
                    

      System.out.println("insert : " + query);
     try{ // insert new employee +  dependent
         Statement stmt = con.createStatement();
         stmt.setQueryTimeout(5);
    	  rc = stmt.executeUpdate(query);
         stmt.close();
     }//end try
      catch(SQLException ex){
         System.out.println("Order ikke oprettet");
        throw new Exception ("Order is not inserted correct");
      }
      return(rc);
	}

	@Override
	public int update(PartOrder dept) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int delete(PartOrder dept) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	private ArrayList<PartOrder> miscWhere(String wClause, boolean retrieveAssociation)
	{
            ResultSet results;
	    ArrayList<PartOrder> list = new ArrayList<PartOrder>();	
		
	    String query =  buildQuery(wClause);
  
            try{ // read the employee from the database
		Statement stmt = con.createStatement();
	 	stmt.setQueryTimeout(5);
	 	results = stmt.executeQuery(query);
	 	
	
		while( results.next() ){
	     	 PartOrder oObj = new PartOrder();
		 oObj = buildPartOrder(results);	
                 list.add(oObj);	
		}//end while
                 stmt.close();     
            }
	 	catch(Exception e){
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return list;
	}
	
	private String buildQuery(String wClause)
	{
	    String query="SELECT quantity, price, productid, orderid  FROM PARTORDER";
		
		if (wClause.length()>0)
			query=query+" WHERE "+ wClause;
			
		return query;
	}
	
	private PartOrder buildPartOrder(ResultSet results)
    {   PartOrder oObj = new PartOrder();
        try{ // the columns from the table emplayee  are used
        	oObj.setQuantity(results.getInt("quantity"));
        	oObj.setPrice(results.getDouble("price"));
        	oObj.setProduct(new Product(results.getString("prductid")));
        	oObj.setOrder(new SalesOrder(results.getString("orderid")));
        	
              
        }
       catch(Exception e)
       {
           System.err.println("error in building the product object");
       }
       return oObj;
    }
	

}
